﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;

namespace P03
{
    public class A10 : IPDAChild
    {
        public void Code(IPDA handler, CodeResult CR, PDACodeUser user, String parameter)
        {
            P03A10 P = (P03A10)handler.SerialApi.Deserialize(typeof(P03A10), parameter, CR.SerialType);

            try
            {
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NChar, 50);
                sps[0].Value = P.FID;

                String sql = "Select A.FID CarFID, A.FBillNo FBillNo,E.FNumber ClientNumber,E.FName ClientName,A.FText Car,A.FDecimal4 PreQty,B.FName ItemName,IsNull(C.FDecimal2,0) BagQty ,D.FText BagJetPre,D.FINTEGER BagJetNumber,D.FINTEGER1 BagJetLength ";
                sql += "FROM KEW_SalesInfo A LEFT JOIN KEW_ProductsEntry B ON A.FBase=B.FID LEFT JOIN KEW_ProductsBagRuleEntry C ON B.FBase1=C.FID ";
                sql += "LEFT JOIN KEW_SalesBathManagerEntry D ON D.FBASE=B.FID ";
                sql += "LEFT JOIN t_Organization E ON A.FBase1=E.FItemID ";
                sql += " WHERE A.FComboBox<>'3' and A.FCheckBox4='0' and A.FID=@FID";
                Recordset rs = handler.Data.ExeProcedure(P.AccountNo, sql, sps);


                if (rs.Enable)
                {
                    BagInfo bc = new BagInfo();

                    if (rs == null || rs.Data.Tables.Count <= 0 || rs.Data.Tables[0].Rows.Count <= 0)
                    {
                        CR.Result = EnumResult.Error;
                        CR.Message = "袋装信息不存在";
                        return;
                    }

                    foreach (DataTable dt in rs.Data.Tables)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            bc.CarFID = dr["CarFID"].ToString();
                            bc.Car = dr["Car"].ToString();
                            bc.PreQty = dr["PreQty"].ToString();
                            bc.ItemName = dr["ItemName"].ToString();
                            bc.BagQty = dr["BagQty"].ToString();
                            bc.FBillNo = dr["FBillNo"].ToString();
                            bc.ClientName = dr["ClientName"].ToString();
                            bc.JetContent = dr["BagJetPre"].ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00");
                            bc.JetContent += dr["ClientNumber"].ToString().Replace("01.", "") +  String.Format("{0:D" + dr["BagJetLength"].ToString() + "}", int.Parse(dr["BagJetNumber"].ToString())) + "@9";
                        }
                    }

                    CR.Result = EnumResult.Normal;
                    CR.Content = handler.SerialApi.Serializer(typeof(BagInfo), bc, CR.SerialType);
                    handler.Message(MessageType.Note, String.Format("操作员{0}对单据{1}进行了袋装信息查询", user.UserName, P.FID));
                }
                else
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "数据库错误";
                }
            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = ex.Message;
                handler.Message(MessageType.Error, String.Format("发生错误:{0}", ex.Message));
            }
        }
    }

    public class P03A10
    {
        public string FID;
        public string AccountNo;
    }

}
